﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AzukiMap.MapSource;
using System.Collections.Generic;
using AzukiMap;
using AzukiMap.Tools.Logger;

namespace AzukiMapUI
{
	//public partial class KaMapProperties : UserControl, IMapSourceProperties
	public partial class KaMapProperties : TileSourceProperties
	{
        KaMapSource _tmpSource;

        public override MapSource MapDataSource
        {
            get
            {
                KaMapSource kmSource = new KaMapSource();
                SetAll(kmSource);
                kmSource.Name = kmSource.MapName + "_" + kmSource.Groups + "_" + kmSource.Layers;
                return kmSource;
            }
        }

        public KaMapSource KmSource
        {
            set { 
                _tmpSource = value;
                SetForm();
            }
        }

		public KaMapProperties()
		{
            _tmpSource = new KaMapSource();
            init();
		}

        public KaMapProperties(KaMapSource kaMapSource)
        {
            _tmpSource = kaMapSource;
            init();
        }

        void init()
        {
			// Required to initialize variables
			InitializeComponent();

            SetForm();
            CheckAllFormData();
        }

        public override bool CheckAllFormData()
        {
            bool isOk1 = CheckServerPaths();
            bool isOk2 = CheckScales();
            bool isOk3 = CheckTileWidth();
            bool isOk4 = CheckTileHeight();
            bool isOk5 = CheckTileDpi();
            bool isOk6 = CheckLayers();
            bool isOk7 = CheckGroups();
            bool isOk8 = CheckSrid();
            bool isOk9 = CheckMapName();

            return (
                isOk1 && isOk2 && isOk3 && isOk4 && isOk5 && isOk6 && isOk7 && isOk8 && isOk9
                );

            /* does not work.
            return (
                CheckServerPaths() &&
                CheckScales() &&
                CheckTileWidth() &&
                CheckTileHeight() &&
                CheckTileDpi() &&
                CheckLayers() &&
                CheckGroups() &&
                CheckSrid()
                );
             */
        }

        void SetForm()
        {
            FormInputControl.SetForm(TbServerPaths, _tmpSource.BaseUris);
            FormInputControl.SetForm(TbScales, _tmpSource.Scales);
            FormInputControl.SetForm(TbLayers, _tmpSource.Layers);
            FormInputControl.SetForm(TbMapName, _tmpSource.MapName);
            FormInputControl.SetForm(TbGroups, _tmpSource.Groups);
            FormInputControl.SetForm(TbSrid, _tmpSource.Srid);
            FormInputControl.SetForm(TbTileWidth, _tmpSource.TileWidth);
            FormInputControl.SetForm(TbTileHeight, _tmpSource.TileHeight);
            FormInputControl.SetForm(TbTileDpi, _tmpSource.Dpi);
        }

        #region set data from form

        void SetAll(KaMapSource kmSource)
        {
            SetServerPathes(kmSource);
            SetScales(kmSource);
            SetLayers(kmSource);
            SetMapName(kmSource);
            SetGroups(kmSource);
            SetSrid(kmSource);
            SetTileWidth(kmSource);
            SetTileHeight(kmSource);
            SetDpi(kmSource);
        }

        void SetServerPathes(KaMapSource kmSource)
        {
            if ( CheckServerPaths()) 
                FormInputControl.SetListDataFromForm(kmSource.BaseUris, TbServerPaths.Text);
        }

        void SetScales(KaMapSource kmSource)
        {
            if (CheckScales())
                FormInputControl.SetListDataFromForm(kmSource.Scales, TbScales.Text);
            kmSource.Scales.Sort();
        }

        void SetMapName(KaMapSource kmSource)
        {
            if (CheckMapName())
                kmSource.MapName = TbMapName.Text;
        }

        void SetLayers(KaMapSource kmSource)
        {
            if (CheckLayers())
                FormInputControl.SetListDataFromForm(kmSource.Layers, TbLayers.Text);
        }

        void SetGroups(KaMapSource kmSource)
        {
            if (CheckGroups())
                FormInputControl.SetListDataFromForm(kmSource.Groups, TbGroups.Text);
        }

        void SetSrid(KaMapSource kmSource)
        {
            if (CheckSrid())
                kmSource.Srid = int.Parse(TbSrid.Text);
        }

        void SetTileWidth(KaMapSource kmSource)
        {
            if (CheckTileWidth())
                kmSource.TileWidth = int.Parse(TbTileWidth.Text);
        }

        void SetTileHeight(KaMapSource kmSource)
        {
            if (CheckTileHeight())
                kmSource.TileHeight = int.Parse(TbTileHeight.Text);
        }

        void SetDpi(KaMapSource kmSource)
        {
            if (CheckTileDpi())
                kmSource.Dpi = int.Parse(TbTileDpi.Text);
        }

        #endregion

        #region check form data

        bool CheckServerPaths()
        {
            string inputStr = TbServerPaths.Text;
            return FormInputControl.CheckUriList(inputStr, IconErrorServerPaths);
        }

        bool CheckScales()
        {
            string inputStr = TbScales.Text;
            return FormInputControl.CheckDoubleList(inputStr, IconErrorScales);
        }

        bool CheckMapName()
        {
            string inputStr = TbMapName.Text;
            return FormInputControl.HasData(inputStr, IconErrorMapName);
        }

        bool CheckTileWidth()
        {
            string inputStr = TbTileWidth.Text;
            return FormInputControl.CheckNaturalNumber(inputStr, IconErrorTileWidth);
        }

        bool CheckTileHeight()
        {
            string inputStr = TbTileHeight.Text;
            return FormInputControl.CheckNaturalNumber(inputStr, IconErrorTileHeight);
        }

        bool CheckTileDpi()
        {
            string inputStr = TbTileDpi.Text;
            return FormInputControl.CheckNaturalNumber(inputStr, IconErrorTileDpi);
        }

        bool CheckLayers()
        {
            /*
            string inputStr = TbLayers.Text;
            return CheckLayers(inputStr, IconErrorLayers);
             */
            FormInputControl.HideElement(IconErrorLayers);
            return true;
        }

        bool CheckGroups()
        {
            /*
            string inputStr = TbGroups.Text;
            return CheckGroups(inputStr, IconErrorGroups);
             */
            FormInputControl.HideElement(IconErrorGroups);
            return true;
        }

        bool CheckSrid()
        {
            string inputStr = TbSrid.Text;
            return FormInputControl.CheckNaturalNumber(inputStr, IconErrorSrid);
        }

        #endregion

        #region lostFocus event handlers

        void TbServerPaths_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckServerPaths();
        }

        void TbScales_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckScales();
        }

        void TbTileWidth_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckTileWidth();
        }

        void TbTileHeight_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckTileHeight();
        }

        void TbTileDpi_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckTileDpi();
        }

        void TbLayers_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckLayers();
        }

        void TbGroups_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckGroups();
        }

        void TbSrid_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckSrid();
        }

        void TbMapName_LostFocus(object sender, RoutedEventArgs e)
        {

        }

        #endregion

	}
}